/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-10
*/
package com.rzico.basics.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Company;
import com.rzico.basics.entity.CompanyEmployee;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.model.CompanyEmployeeVo;
import com.rzico.basics.service.CompanyEmployeeService;
import com.rzico.basics.service.CompanyService;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业员工控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-05-10
 */
@Api(description = "企业员工接口")
@RestController
@RequestMapping("/member/companyEmployee")
public class CompanyEmployeeMemberController extends BaseController {

    @Autowired
    private CompanyEmployeeService companyEmployeeService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private MemberService memberService;

    /**
     * 检查是否绑定
     *
     * @return
     */
    @ApiOperation("检查是否绑定")
    @GetMapping("/isBind")
    public CommResult<CompanyEmployee> isBind(String mchId) {

        Member member = memberService.getCurrent();

        Enterprise enterprise = enterpriseService.findByMch(mchId);

        Map<String,Object> params = new HashMap<>();
        params.put("status",1);
        params.put("memberId",member.getId());
        params.put("enterpriseId",enterprise.getId());

        List<CompanyEmployee> employeeList = companyEmployeeService.selectList(params);
        if (employeeList.size()==1) {
            return CommResult.success(true);
        }
        if (employeeList.size()>1) {
            return CommResult.error("编号重复");
        }

        return CommResult.success(false);

    }

    /**
     * 绑定企业员工
     *
     * @return
     */
    @ApiOperation("绑定企业员工")
    @PostMapping("/bind")
    public CommResult<CompanyEmployee> bind(String mchId,String empNo,String empName) {

        Member member = memberService.getCurrent();
        Enterprise enterprise = enterpriseService.findByMch(mchId);

        Map<String,Object> params = new HashMap<>();
        params.put("enterpriseId",enterprise.getId());
        params.put("memberId",member.getId());
        List<CompanyEmployee> employeeList = companyEmployeeService.selectList(params);
        for (CompanyEmployee employee:employeeList) {
            employee.setMemberId(0l);
            companyEmployeeService.updateByPrimaryKeySelective(employee);
        }

        params.clear();
        params.put("status",1);
        params.put("empNo",empNo);
        params.put("empName",empName);
        params.put("enterpriseId",enterprise.getId());

        employeeList = companyEmployeeService.selectList(params);
        if (employeeList.size()==0) {
            return CommResult.error("无效编号");
        }
        if (employeeList.size()>1) {
            return CommResult.error("编号重复");
        }


        CompanyEmployee companyEmployee = employeeList.get(0);
        companyEmployee.setMemberId(member.getId());
        companyEmployeeService.updateByPrimaryKeySelective(companyEmployee);

        return CommResult.success();

    }

}
